package de.Objects;

import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.PointLight;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.SpotLight;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;

public class soroundingWorld extends BranchGroup {

	Appearance apper = new Appearance();
	Appearance apperFloor = new Appearance();
	BranchGroup root = new BranchGroup();

	public soroundingWorld() {
		addLight();

// ////////create Material
		Color3f ambient = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f emissive = new Color3f(0.3f, 0.3f, 0.3f);
		Color3f diffuse = new Color3f(0.2f, 0.2f, 0.2f);
		Color3f specular = new Color3f(0.8f, 0.8f, 0.8f);

		Material material = new Material(ambient, emissive, diffuse, specular,
				100.0f);
		material.setLightingEnable(true);

// //////// Create The Sorounding enviroment
		TextureLoader loaderBase = new TextureLoader("textures/world.jpg", null);
		ImageComponent2D imageBase = loaderBase.getImage();
		if (imageBase == null) {
			System.out.println("failed loading texture");
		} else {
			Texture2D textTureBase = new Texture2D(Texture.BASE_LEVEL,
					Texture.RGBA, imageBase.getWidth(), imageBase.getHeight());
			textTureBase.setImage(0, imageBase);
			apper.setTexture(textTureBase);

			TextureAttributes texAttrBase = new TextureAttributes();
			texAttrBase.setTextureMode(TextureAttributes.MODULATE);
			apper.setTextureAttributes(texAttrBase);
		}

		Sphere sp1 = new Sphere(300.0f, Primitive.GENERATE_NORMALS_INWARD
				| Primitive.GENERATE_TEXTURE_COORDS, 100, apper);
		TransformGroup tgWorld = new TransformGroup();
		tgWorld.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		tgWorld.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		root.addChild(tgWorld);
		tgWorld.addChild(sp1);

// ////////rotating the world
		Alpha a = new Alpha(-1, 100000);
		Transform3D rotW = new Transform3D();
		rotW.rotX(Math.PI / 2);
		RotationInterpolator rotInterPol = new RotationInterpolator(a, tgWorld);
		rotInterPol.setTransformAxis(rotW);
		rotInterPol.setSchedulingBounds(new BoundingSphere(new Point3d(0.0f,
				0.0f, 0.0f), 1000.0f));
		tgWorld.addChild(rotInterPol);

// //////// Create the Floor
		TextureLoader loaderFloor = new TextureLoader("textures/snow1G.jpg",
				null);
		ImageComponent2D imageFloor = loaderFloor.getImage();
		if (imageFloor == null) {
			System.out.println("failed loading texture");
		} else {
			Texture2D textTureFloor = new Texture2D(Texture.BASE_LEVEL,
					Texture.RGBA, imageFloor.getWidth(), imageFloor.getHeight());
			textTureFloor.setImage(0, imageFloor);
			apperFloor.setTexture(textTureFloor);

			TextureAttributes texAttrFloor = new TextureAttributes();
			texAttrFloor.setTextureMode(TextureAttributes.MODULATE);
			apperFloor.setTextureAttributes(texAttrFloor);
			apperFloor.setMaterial(material);
		}
		Cylinder floor = new Cylinder(300.0f, 0.1f, Primitive.GENERATE_NORMALS
				| Primitive.GENERATE_TEXTURE_COORDS, 100, 100, apperFloor);
		Transform3D translationFloor = new Transform3D();
		translationFloor.setTranslation(new Vector3f(0.0f, -10.2f, 0.0f));
		TransformGroup tgFloor = new TransformGroup(translationFloor);
		root.addChild(tgFloor);
		tgFloor.addChild(floor);

		super.addChild(root);
	}

	public void addLight() {
		SpotLight lightS1 = new SpotLight();
		lightS1.setDirection(new Vector3f(0.0f, 0.0f, 0.0f));
		lightS1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
		lightS1.setPosition(0.0f, 10.0f, 0.0f);
		lightS1.setSpreadAngle(10.0f);
		lightS1.setInfluencingBounds(new BoundingSphere(new Point3d(0.0f, 0.0f,
				0.0f), 30.0f));
		root.addChild(lightS1);
	}
}